import random
import time

file = "./datas/SerableQueen.json"  # 这里存储的是每个势力所对应的用户


class UserJoin:
    def __init__(self, _init):
        self._core = _init

    @property
    def _dict(self) -> dict:
        self._core.load()
        return self._core.core

    def has_play(self) -> bool:
        """
        查看某个实例是否存在用户
        :return:
        """

    def join_user(self, user, power):
        """
        用户加入
        :param user 用户token
        :param power 用户选择的势力

        :return:
        """
        self._core.load()  # 加载
        self._dict[power] = user
        self._core.save()  # 保存
        self._core.load()  # 在进行加载

    def validate(self, user):
        """
        判断用户的token是否为合法
        :param user:
        :return: 【布尔值 【token, powername】】
        """

        for powername, token in self._dict.items():
            if token == user:
                return True, [token, powername]
        return False, [None, None]

    def random_token_generate(self):
        """
        随机生成token
        :return:
        """
        time_zine = time.time()
        rand_code = random.randint(1, 200)
        print(time_zine, rand_code)
        rand_code = f"{time_zine}-{rand_code}"
        rand_code = str(hash(rand_code))
        return rand_code

    def get_all_user(self):
        """
        获取所有的用户
        :return:
        """
        ret = [user for i, user in self._dict.items() if user is not None]
        return ret

    def get_power_hasuser(self):
        """
        获取用户存在的势力
        :return:
        """
        ret = [i for i, user in self._dict.items() if user is not None]
        return ret
